package paidaxing.blog.post.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import paidaxing.blog.post.service.TagsService;
import paidaxing.blog.post.dto.request.TagRequestDTO;
import paidaxing.blog.post.dto.response.TagResponseDTO;
import paidaxing.blog.common.result.Result;
import paidaxing.blog.common.result.PageResult;
import paidaxing.blog.post.dto.PostDTO;

/**
 * 标签控制器
 */
@Slf4j
@RestController
@RequestMapping("/tags")
public class TagController {

    @Autowired
    private TagsService tagsService;

    /**
     * 获取标签列表
     * 
     * @param page 页码
     * @param size 每页大小
     * @return 标签列表
     */
    @GetMapping
    public Result<PageResult<TagResponseDTO>> getTags(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        if (log.isInfoEnabled()) {
            log.info("获取标签列表，页码: {}, 大小: {}", page, size);
        }
        
        PageResult<TagResponseDTO> pageResult = tagsService.getTags(page, size);
        return Result.success("获取标签列表成功!", pageResult);
    }

    /**
     * 创建标签
     * 
     * @param tagRequestDTO 标签请求DTO
     * @return 创建的标签
     */
    @PostMapping
    public Result<TagResponseDTO> createTag(@RequestBody TagRequestDTO tagRequestDTO) {
        if (log.isInfoEnabled()) {
            log.info("创建标签: {}", tagRequestDTO);
        }
        
        TagResponseDTO responseDTO = tagsService.createTag(tagRequestDTO);
        return Result.success("创建标签成功!", responseDTO);
    }

    /**
     * 更新标签
     * 
     * @param tagId 标签ID
     * @param tagRequestDTO 标签请求DTO
     * @return 更新后的标签
     */
    @PutMapping("/{tagId}")
    public Result<TagResponseDTO> updateTag(
            @PathVariable Long tagId,
            @RequestBody TagRequestDTO tagRequestDTO) {
        if (log.isInfoEnabled()) {
            log.info("更新标签: {}, ID: {}", tagRequestDTO, tagId);
        }
        
        TagResponseDTO responseDTO = tagsService.updateTag(tagId, tagRequestDTO);
        if (responseDTO == null) {
            return Result.failed("标签不存在");
        }
        return Result.success("更新标签成功!", responseDTO);
    }

    /**
     * 删除标签
     * 
     * @param tagId 标签ID
     * @return 删除结果
     */
    @DeleteMapping("/{tagId}")
    public Result<String> deleteTag(@PathVariable Long tagId) {
        if (log.isInfoEnabled()) {
            log.info("删除标签，ID: {}", tagId);
        }
        
        boolean result = tagsService.deleteTag(tagId);
        if (!result) {
            return Result.failed("标签不存在");
        }
        return Result.success("删除标签成功!");
    }

    /**
     * 获取标签下的文章
     * 
     * @param tagId 标签ID
     * @param page 页码
     * @param size 每页大小
     * @return 标签下的文章列表
     */
    @GetMapping("/{tagId}/posts")
    public Result<PageResult<PostDTO>> getPostsByTag(
            @PathVariable Long tagId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        if (log.isInfoEnabled()) {
            log.info("获取标签下的文章，标签ID: {}, 页码: {}, 大小: {}", tagId, page, size);
        }
        
        PageResult<PostDTO> pageResult = tagsService.getPostsByTag(tagId, page, size);
        return Result.success("获取标签下的文章成功!", pageResult);
    }
}